import modelExtend from 'dva-model-extend'
import { lwjPageModel } from 'utils/model'
import { query, create, accept, deny, canceledList } from 'services/orderCancel'
import { Modal, notification } from 'antd';

export default modelExtend(lwjPageModel, {
  namespace: 'orderCancel',
  state: {
    currentItem: {},
    createModalVisible: false,
    auditModalVisible: false
  },
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({pathname}) => {
        if (pathname === '/order/canceling') {
          dispatch({type: 'query', payload: {}})
        }

        if (pathname === '/order/canceled') {
          dispatch({type: 'canceledList', payload: {}})
        }
      })
    }
  },
  effects: {
    * query ({
               payload
             }, {select, call, put}) {
      yield put({
        type: 'asyncData',
        payload: {
          ...payload
        }
      })
      const pageOptions = yield select(({orderCancel}) => orderCancel.pageOptions)
      const queryParam = yield select(({orderCancel}) => orderCancel.queryParam)
      const queryPayload = {
        ...pageOptions,
        queryParam: {
          ...queryParam
        },
      }
      const data = yield call(query, queryPayload)
      yield put({
        type: 'querySuccess',
        payload: {
          ...data
        }
      })
    },

    * canceledList ({
               payload
             }, {select, call, put}) {
      yield put({
        type: 'asyncData',
        payload: {
          ...payload
        }
      })
      const pageOptions = yield select(({orderCancel}) => orderCancel.pageOptions)
      const queryParam = yield select(({orderCancel}) => orderCancel.queryParam)
      const queryPayload = {
        ...pageOptions,
        queryParam: {
          ...queryParam
        },
      }
      const data = yield call(canceledList, queryPayload)
      yield put({
        type: 'querySuccess',
        payload: {
          ...data
        }
      })
    },

    * create ({payload}, {select, call, put}) {
      const res = yield call(create, payload)

      if (res.success) {
        yield put({
          type: 'hideModal',
          payload: {}
        })
      } else {
        notification.open({
          message: '失败',
          description: `取消失败`,
          duration: 2,
        });
      }
    },

    * accept ({payload}, {select, call, put}) {
      const res = yield call(accept, payload)

      if (res.success) {
        yield put({
          type: 'hideModal',
          payload: {}
        })
      }
    },

    * deny ({payload}, {select, call, put}) {
      const res = yield call(deny, payload)

      if (res.success) {
        yield put({
          type: 'hideModal',
          payload: {}
        })
      }
    }

  },
  reducers: {
    showCreateModal (state, {payload}) {
      state.createModalVisible = true
      state.currentItem = payload

      return {
        ...state
      }
    },

    showAuditModal (state, {payload}) {
      state.auditModalVisible = true
      state.currentItem = payload

      return {
        ...state
      }
    },

    hideModal (state, {payload}) {
      state.createModalVisible = false
      state.auditModalVisible = false

      return {
        ...state
      }
    },

    expandFilter (state, {payload}) {
      return {
        ...state,
        isExpand: true
      }
    },
    unExpandFilter (state, {payload}) {
      return {
        ...state,
        isExpand: false
      }
    }
  }
})
